'use strict';

const Service = require('egg').Service;

class DetailsService extends Service {
    async getDetails(id) {
        const { app } = this;
        const result = await app.mysql.get('goods', { id });
        result.urls = result.urls.split(',');
        result.urls = result.urls.map((item, index) => {
            return {
                src: item,
                id: index
            }
        });
        result.urlss = result.urlss.split(',');
        result.urlss = result.urlss.map((item, index) => {
            return {
                src: item,
                id: index
            }
        });
        result.shop = await app.mysql.get('shop', { id: result.shop_id });
        delete result.shop_id;
        result.list = await app.mysql.get('good_list', { id: result.list_id });
        result.dataList = await app.mysql.select('goods', {
            where: { list_id: result.list_id},
            limit:11,
            offset:0
        });
        const index= (result.dataList.map(item=>item.id)).indexOf(result.id)
        index!==-1?result.dataList.splice(index,1):result.dataList.splice(10,1);
        delete result.list_id;
        return result;
    }
}

module.exports = DetailsService;
